#include "Tower.h"
#include "RootScene.h"
#include "Enemy.h"
USING_NS_CC;

CTower * CreateTower( cocos2d::CCLayer* pLayer, cocos2d::CCPoint Position )
{
	CTower* pTower = new CTower();
	if( pTower )
	{
		pTower->setPosition( Position );
		pTower->setAttackRange( 70 );
		pTower->setAttackDamage( 10 );
		pTower->setAttackSpeed( 50 );
		pTower->setCost( 100 );
		pTower->setCoolDown( 1.5f );

		pLayer->addChild( pTower );

		pTower->scheduleUpdate();
	}

	return pTower;
}

CBullte * CreateBullte( CCLayer* pScene, CCPoint Position, CCPoint Direction, float fSpeed )
{
	CBullte* pBullte = new CBullte();
	if( pBullte )
	{
		CCSprite *bullet = CCSprite::create("bullet.png");

		pBullte->setSpeed( fSpeed );
		pBullte->setPosition(Position);
		pBullte->setDirection(Direction);
		pBullte->setRadius( 1 );
		pBullte->addChild( bullet );
		pScene->addChild( pBullte );

		pBullte->scheduleUpdate();
	}

	return pBullte;
}

CTower::CTower()
	: mAttackRange( 0.0f )
	, mAttackDamage( 0.0f )
	, mAttackSpeed( 0.0f )
	, mCost( 0.0f )
	, mCoolDown( 0.0f )
	, mCoolDownMeasure( 0.0f )
{
	mSprite = CCSprite::create("tower.png");
	addChild( mSprite );
}

CTower::~CTower(void)
{
}

void CTower::update( float dt )
{
	CRootScene* pScene = (CRootScene*)getParent();
	if( pScene )
	{
		//float fMinDistance = std::numeric_limits< float >::max();
		CCObject* pObject = NULL;
		CEnemy* pTarget = NULL;
		CCARRAY_FOREACH( pScene->getEnemyArray(), pObject )
		{
			CEnemy* pEnemy = (CEnemy*)pObject;
			if( pEnemy )
			{
				float fDistance = ccpDistance( pEnemy->getPosition(), getPosition() );
				if( fDistance <= mAttackRange )
				{
					pTarget = pEnemy;
					break;
				}
			}
		}

		if( pTarget )
		{
			CCPoint direction = ccpNormalize( ccpSub( pTarget->getPosition(), getPosition() ) );

			mSprite->setRotation(CC_RADIANS_TO_DEGREES(atan2(direction.y, - direction.x)) + 90);
			mCoolDownMeasure -= dt;
			if( mCoolDownMeasure < 0.0f )
				fire( direction );
		}
	}
}

void CTower::fire( CCPoint direction )
{
	CreateBullte( (CRootScene*)getParent(), ccpAdd( getPosition(), ccpMult( direction, 10 ) ), direction, mAttackSpeed );
	mCoolDownMeasure = mCoolDown;
}

void CTower::draw( void )
{
#ifdef COCOS2D_DEBUG
	ccDrawColor4F( 255, 255, 255, 255 );
	ccDrawCircle( getPosition(), mAttackRange, 360, 30, false );
#endif
	CCNode::draw();
}

//////////////////////////////////////////////////////////////////////////
// CBullte Class
//////////////////////////////////////////////////////////////////////////
CBullte::CBullte()
{

}

CBullte::~CBullte()
{

}

void CBullte::update( float dt )
{
	setPosition( ccpAdd( getPosition(), ccpMult( ccpMult( mDirection, mSpeed ), dt ) ) );
	CCObject* pObject = NULL;
	CRootScene* pScene = (CRootScene*)getParent();
	CCARRAY_FOREACH( pScene->getEnemyArray(), pObject )
	{
		CEnemy* pEnemy = (CEnemy*)pObject;
		if( judgeCollision( getPosition(), mRadius, pEnemy->getPosition(), 1 ) )
		{
			cleanup();
			release();
			return;
		}
	}

	if( getPositionX() < 0 || getPositionY() < 0 )
	{
		cleanup();
		release();
	}
}